import { defineStore } from 'pinia'
import { http } from '@/utils/http'
import { createPinia } from 'pinia'
import { createPersistedState } from 'pinia-plugin-persistedstate' // 数据持久化

const store = createPinia()
store.use(
    createPersistedState({
        storage: {
            getItem: uni.getStorageSync,
            setItem: uni.setStorageSync,
        },
    }),
)
export default store

export const useStore = defineStore("main", {
    state() {
        return {
            token: '',
            member: {} as any, // 会员信息
            notice_unread: 0, // 会员通知数量
            store: {} as any, // 店铺信息
            store_worker: {} as any, // 员工信息
            store_role: '', // 店铺角色

            share_title: '', // 分享标题
            share_desc: '', // 分享描述
            share_img: '', // 分享图片
        };
    },
    actions: {
        setMemberData(fun = function (data: any) { }) {
            http.get('/member/user/me').then((res: any) => {
                if (res.code == 200) {
                    if (res.data.member) this.member = res.data.member
                    if (res.data.notice_unread != undefined) this.notice_unread = res.data.notice_unread
                    if (res.data.store) this.store = res.data.store
                    if (res.data.store_worker) this.store_worker = res.data.store_worker
                    if (res.data.store_role != undefined) this.store_role = res.data.store_role
                    fun(res.data)
                }
            })
        },
    },
    persist: true, // 开启后对 state 的数据读写都将持久化
});